<?php

include_once '../../LMSModel/LMSValidateUserModel/LMSValidateUserModel.php';

class LMSValidateUserController
{
    private $mName;
    private $mPassword;
	private $mUserModelTable;
	
	/** method or function name
	* description
	* parameters 
	* return type
	*/
	function __construct()
	{
		//initiazation can be done
		$this->mName = $_REQUEST['username'];
		$this->mPassword = $_REQUEST['password'];
		$this->getUserModel();
	}
	
	function setSession()
	{
		session_start();
		// store session data
		$_SESSION['ID']=$this->mName;
	}
	
	private function getUserModel()
	{
		$validateUserModelObject = new LMSValidateUserModel();
		$this->mUserModelTable = $validateUserModelObject->getUserModel();
	}
	
	public function validateUser()
	{
		$flag = 0;
		
		while($row = mysql_fetch_array($this->mUserModelTable))
		{
			if(($row['username'] == $this->mName)&&($row['password'] == $this->mPassword))
			{
				if($row['role'] == 0)
				{
					$flag = 1;
				}
				else
				{
					$flag = 2;
				}
				$this->setSession();
				break;
			}
		}
		
		if($flag == 1)
		{
			header("Location:../../LMSView/LMSHomePageView/LMSAdminHomePageView.php");
		}
		else if($flag == 2)
		{
			header("Location:../../LMSView/LMSHomePageView/LMSStudentHomePageView.php"); 
		}
		else
		{
			echo 'Invalid username or password';
		}
	}
}

$validateUserControllerObject = new LMSValidateUserController();
$validateUserControllerObject->validateUser();